<template>
  <!-- 救援审核 -->
  <div>
    <TopFilter ref="topFilter" :list="topFilterList" :cols="3" labelWidth="120" @filterChange="changeHandle" @onCollapse="collapseHandle" @resetChange="resetHandle"></TopFilter>
    <button-area :style="{ paddingLeft: '80px' }"></button-area>
    <FilterTable
      ref="table"
      columnsRef="repair-labourHour-index"
      :isSelectColumn="false"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectData"
    >
      <template slot="controls">
        <AjaxButton size="small" type="primary" icon="el-icon-download" :click="outExportData.bind()">下载</AjaxButton>
      </template>
    </FilterTable>

    <!--抽屉 -->
    <Drawer :visible.sync="actions.visible" :title="actions.title" width="90%" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <addDialog :type="actions.type" :formData="formData" @close="close" />
    </Drawer>
    <BaseDialog :visible.sync="selectDelearFlag" :title="'服务中心选择'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <DealerList @close="Delearclose" @save="Delearsave" selectionType="multiple"></DealerList>
    </BaseDialog>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import dictCode from '@/utils/dictCode';
import { selectDetail,getListByPage,getOutExport } from '@/api/repair/repairBusiness/rescueService/index.js';
import addDialog from './addDialog';
import searchDealerPanel from '@/pages/repair/mileageDateChange/mileageApprove/searchDealerPanel';
import { exportExcelFile } from '@/utils/index';
import DealerList from '@/pages/claimDemage/tools/DealerList.vue';
export default {
  name: 'rescue-editDialog',
  components: { addDialog, searchDealerPanel, DealerList },
  mixins: [dictionary],
  data() {
    this.allSeries = {};
    return {
      fetchParams: {},
      fetchApi: getListByPage,
      columns: this.createTableColumns(),
      formData: {},
      tableData: [],
      topFilterList: this.createTopFilterList(),
      actions: { type: '', title: '', visible: false, data: null },
      allDealer: [],
      selectDelearFlag: false,
      dealerCodeSearch: "",
      dealerCompanyCode: [],
      dealerData: [],
      selectDealerData: []
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '救援申请单号',
          fieldName: 'outNo',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入...'
        },
        {
          type: 'RANGE_DATE',
          label: '创建日期',
          style: { minWidth: '220px' },
          fieldName: 'createdStartDate|createdEndDate'
        },
        {
          type: 'RANGE_DATE',
          label: '提报日期',
          style: { minWidth: '220px' },
          fieldName: 'applyStartDate|applyEndDate'
        },
        {
          type: 'SELECT',
          label: '单据状态',
          fieldName: 'outStatus',
          placeholder: '请选择...',
          itemList: [
            { text: "已提交",value: 55361002 },
            { text: "审核通过",value: 55361003 },
            { text: "审核驳回",value: 55361004 },
            { text: "已过期",value: 55361005 },
            { text: "已索赔",value: 55361006 },
          ]
        },
        {
          type: 'INPUT',
          label: '服务中心',
          render: (p) => {
            return (
              <div>
                <el-select v-model={this.selectDealerData} disabled={true} multiple placeholder="请选择">
                  {this.dealerData.map((item) => (
                    <el-option key={item.dealerCode} label={item.dealerShortname} value={item.dealerCompanyCode} />
                  ))}
                </el-select>
                <multiuse-button style="position: absolute;right: 0;height:100%" icon="el-icon-search" onClick={() => this.selectDelearFlag = true} />
              </div>
            );
          },
        },
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          minWidth: '100',
          render: (props, h) => {
            return (
              <div>
                <el-button
                 size="mini"
                 type="text"
                 disabled={props.row.outStatus === 55361002 ? false : true}
                 onClick={() => this.emitFun(props.row, 'edit')}>
                  审核
                </el-button>
                <el-divider direction="vertical" />
                <el-button size="mini" type="text" onClick={() => this.emitFun(props.row,'show')}>
                  明细
                </el-button>
              </div>
            );
          }
        },
        {
          title: '救援申请单号',
          dataIndex: 'outNo',
          sorter: true
        },
        {
          title: '单据状态',
          dataIndex: 'outStatus',
          sorter: true,
          filterType: 'checkbox',
          dictItems: this.createDictList(5536)
        },
        {
          title: '服务中心代码',
          dataIndex: 'dealerCode',
          width: 150,
          sorter: true
        },
        {
          title: '服务中心名称',
          dataIndex: 'dealerName',
          width: 150,
          sorter: true
        }, 
       {
          title: 'VIN',
          dataIndex: 'vin',
          width: 150,
          sorter: true
        },
        {
          title: '车牌号',
          dataIndex: 'vhlPlate',
          width: 150,
          sorter: true
        },
        {
          title: '电机号',
          dataIndex: 'engineCode',
          width: 150,
          sorter: true
        },
        {
          title: '车型',
          dataIndex: 'vehicleType',
          width: 150,
          sorter: true
        },
        {
          title: '创建日期',
          dataIndex: 'createdAt',
          sorter: true,
          width: 150,
          dateFormat: 'yyyy-MM-dd HH:mm:ss'
        },
        {
          title: '提报日期',
          dataIndex: 'applyAt',
          sorter: true,
          width: 150,
          dateFormat: 'yyyy-MM-dd HH:mm:ss'
        },
      ];
    },
    async emitFun(val, type) {
      let res = await selectDetail({ outId: val.outId });
      if (res.resultCode == 200) {
        this.formData = res.data;
        this.actions = { type: type, visible: true, title: '外出救援' };
      }
    },
    selectData(val) {
      this.tableData = val;
    },
    changeHandle(val) {
      // 搜索和重置都走
      let params = { ...this.fetchParams, ...val };
      if (params.createdEndDate) {
        params.createdEndDate = params.createdEndDate.split(' ')[0] + ' 23:59:59';
      }
      if (params.applyEndDate) {
        params.applyEndDate = params.applyEndDate.split(' ')[0] + ' 23:59:59';
      }
      if (params.dealerName) {
        params.ownerCode = this.dealerCodeSearch;
      }
      this.fetchParams = params;
      this.fetchParams = Object.assign({}, this.fetchParams, { dealerCode: this.searchDealerCode, dealerCompanyCode: this.selectDealerData.length ?  this.selectDealerData : null}, { xhrAbort: false });
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    async close(val, type) {
      this.actions.visible = val;
      if (type === 'refresh') {
        const [err, data] = await this.$refs.topFilter.GET_FORM_DATA();
        let params = { ...this.fetchParams, ...val };
        if (params.ownerParCode && params.ownerParCode.length) {
          params.ownerCode = this.topFilterList.find(x => x.fieldName === 'ownerCode').initialValue;
        } else {
          params.ownerCode = undefined;
        }
        this.fetchParams = params;
      }
    },
    async outExportData(){
      await getOutExport(this.fetchParams).then(res => {
        console.log(res);
        if (res.status !== 'failure') {
          exportExcelFile(res.data, '救援单信息');
        } else {
          notifyAction('导出失败', 'error');
        }
      });
    },
    SelectDelear() {
      this.selectDelearFlag = true;
    }, 
    //关闭
    Delearclose() {
      this.selectDelearFlag = false;
    },
    resetHandle(val) {
      this.dealerData = [];
      this.selectDealerData = [];
      this.fetchParams = Object.assign({}, this.fetchParams, val, { dealerCompanyCode: null }, { xhrAbort: false });
    },
    //保存
    Delearsave(obj) {
      console.log('选择数据传递', obj);
      this.dealerData = obj;
      this.selectDealerData = [];
      obj.forEach(item => {
        this.selectDealerData.push(item.dealerCompanyCode);
      });
      this.selectDelearFlag = false;
    }
  }
};
</script>

<style lang="less" scoped></style>
